<template>
  <!-- 查询表单 -->
  <el-form
    ref="formSearchRef"
    label-width="100px"
    :inline="true"
    :model="formSearch"
    class="data-grid-search-form"
  >
    <el-row>
      <el-col :lg="6" :md="8" :sm="12" :xs="24">
        <el-form-item :label="t('account.account')">
          <el-input v-model="formSearch.account" clearable> </el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="6" :md="8" :sm="12" :xs="24">
        <el-form-item :label="t('account.name')">
          <el-input v-model="formSearch.name" clearable> </el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="6" :md="8" :sm="12" :xs="24">
        <el-form-item :label="t('signup.email')">
          <el-input v-model="formSearch.emailMask" clearable> </el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="6" :md="8" :sm="12" :xs="24" v-if="searchInputExpland">
        <el-form-item :label="t('signup.reviewStatus')">
          <el-select v-model="formSearch.status" clearable>
            <el-option :label="t('signup.status0')" value="0"></el-option>
            <el-option :label="t('signup.status1')" value="1"></el-option>
            <el-option :label="t('signup.status2')" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :lg="6" :md="8" :sm="12" :xs="24" v-if="searchInputExpland">
        <el-form-item :label="t('common.startDate')">
          <el-date-picker
            v-model="formSearch.createTimeBegin"
            type="datetime"
            :shortcuts="shortcuts"
            clearable
            class="fix-el-date-picker-width"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :lg="6" :md="8" :sm="12" :xs="24" v-if="searchInputExpland">
        <el-form-item :label="t('common.to')">
          <el-date-picker
            v-model="formSearch.createTimeEnd"
            type="datetime"
            :shortcuts="shortcuts"
            clearable
            class="fix-el-date-picker-width"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>

      <el-col :lg="6" :md="8" :sm="12" :xs="24">
        <el-form-item style="float: right">
          <el-button :icon="Search" type="primary" @click="handleSearch">{{
            t("common.searchButton")
          }}</el-button>
          <el-button
            :icon="RefreshLeft"
            @click="formSearchRef.value.resetFields()"
            >{{ t("common.resetButton") }}</el-button
          >
          <search-input-expland
            @on-expland-change="(v:boolean)=>{ searchInputExpland=v}"
          ></search-input-expland>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>

  <data-grid
    ref="dataGridRef"
    id="admin-account-signup-grid"
    row-key="accountSignupId"
    defaultSort="descending"
    defaultSortBy="createTime"
    :request="handleLoadGrid"
    :columns="state.columns"
    :buttons="state.buttons"
  >
    <template #button-primary="button">
      <!-- <el-button size="mini" :icon="Download">导出日志</el-button> -->
    </template>

    <template #gender="slotProps">
      <el-tag size="small" type="info" v-if="slotProps.row.gender == 0">
        {{ t("account.genderFemale") }}</el-tag
      >
      <el-tag size="small" type="info" v-if="slotProps.row.gender == 1">
        {{ t("account.genderMale") }}</el-tag
      >
      <el-tag size="small" type="info" v-if="slotProps.row.gender == 2">
        {{ t("account.genderUnknown") }}</el-tag
      >
    </template>

    <template #status="slotProps">
      <el-tag size="small" v-if="slotProps.row.status == 0">
        {{ t("signup.status0") }}</el-tag
      >
      <el-tag size="small" v-if="slotProps.row.status == 1" type="success">
        {{ t("signup.status1") }}</el-tag
      >
      <el-tag size="small" v-if="slotProps.row.status == 2" type="danger">
        {{ t("signup.status2") }}</el-tag
      >
    </template>

    <template #operate="slotProps">
      <el-space :size="8">
        <el-link
          type="primary"
          @click="handleView(slotProps.row)"
          v-if="slotProps.row.status == 1"
          >{{ t("common.viewButton") }}</el-link
        >
        <el-link
          type="primary"
          @click="handleReview(slotProps.row)"
          v-if="slotProps.row.status != 1"
          >{{ t("signup.review") }}</el-link
        >
      </el-space>
    </template>
  </data-grid>

  <view-form ref="viewFormRef"></view-form>
  <edit-form ref="editFormRef" @on-submit-success="onSubmitSuccess"></edit-form>
</template>
<script setup lang="ts">
import { reactive, ref, toRefs, onBeforeMount } from "vue";
import DataGrid from "@/components/DataGrid.vue";
import SearchInputExpland from "../../../../components/SearchInputExpland.vue";
import editForm from "./edit.vue";
import viewForm from "./view.vue";
import { Search, RefreshLeft } from "@element-plus/icons-vue";
import utils from "../../../../utils/utils";
import apiAccount from "../../api/account";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
const { t } = useI18n({ useScope: "global" });
const router = useRouter();
// 变量必须和属性 ref 的值相同
const formSearchRef = ref();
const dataGridRef = ref();
const viewFormRef = ref();
const editFormRef = ref();
// 展开收缩
const searchInputExpland = ref(false);

// 查询区域表单数据
const formSearch = reactive({
  account: "",
  name: "",
  emailMask: "",
  status: "",
  createTimeBegin: <Date>(<any>null),
  createTimeEnd: <Date>(<any>null),
});

const shortcuts = [
  {
    text: t("common.aWeekAgo"),
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      formSearch.createTimeBegin = start;
      formSearch.createTimeEnd = end;
      // return [start, end];
    },
  },
  {
    text: t("common.lastMonth"),
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      formSearch.createTimeBegin = start;
      formSearch.createTimeEnd = end;
      // return [start, end];
    },
  },
  {
    text: t("common.last3Months"),
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
      formSearch.createTimeBegin = start;
      formSearch.createTimeEnd = end;
      // return [start, end];
    },
  },
];
const state = reactive({
  // 表格列配置，大部分属性跟el-table-column配置一样
  columns: [
    {
      title: () => t("account.account"),
      field: "account",
      sortable: true,
      resizable: true,
      width: 150,
    },
    {
      title: () => t("account.name"),
      field: "name",
      sortable: true,
      resizable: true,
      width: 150,
    },
    {
      title: () => t("signup.email"),
      field: "emailMask",
      sortable: true,
      resizable: true,
      width: 200,
    },
    {
      title: () => t("account.gender"),
      field: "gender",
      sortable: true,
      resizable: true,
      tdSlot: "gender",
      width: 100,
    },
    {
      title: () => t("signup.reviewStatus"),
      field: "status",
      sortable: true,
      resizable: true,
      tdSlot: "status",
      width: 150,
    },
    {
      title: () => t("signup.description"),
      field: "description",
      sortable: true,
      resizable: true,
    },
    {
      title: () => t("log.createTime"),
      field: "createTime",
      sortable: true,
      resizable: true,
      width: 180,
    },
    {
      title: () => t("common.operateButton"),
      field: "opId",
      tdSlot: "operate",
      width: 150,
    },
  ],
  buttons: [
    {
      type: "primary",
      buttonSlot: "button-primary",
    },
  ],
  row: null,
});

// 加载数据
const handleLoadGrid = async (params: any) => {
  // params 是从表格组件接收的，包含分页和搜索字段，需要补充 query 查询条件信息
  let query = utils.serializeArray(formSearch);
  params.query = JSON.stringify(query);
  let data = { rows: [], total: 0 };
  await apiAccount
    .queryAccountSignupListing(params)
    .then((respons: any) => {
      if (respons != undefined) {
        data = respons.data;
      }
    })
    .catch((err: any) => {
      utils.errorNotification(t("common.submitException"), err);
    });

  // 返回数据给表格组件
  return data;
};

// 新增、编辑页确定按钮回调事件
const onSubmitSuccess = (row: any) => {
  handleSearch();
};
// 查看
const handleView = (row: any) => {
  viewFormRef.value.show(row);
};
// 审核
const handleReview = (row: any) => {
  editFormRef.value.show(row);
};

// 刷新表格
const handleSearch = () => {
  dataGridRef.value.loadGridData();
};
</script>
